Conditions | 1 |
Paths | 1 |
Total Lines | 537 |
Code Lines | 153 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const chai = require('chai'); |
||
107 | describe('Project', function() { |
||
108 | var testProjectId = null; |
||
109 | |||
110 | describe('Create', function() { |
||
111 | it('should create and return a project', async function() { |
||
112 | const client = await getAuthenticatedTestClient(); |
||
113 | const response = await client.createProject({ |
||
114 | title: 'New Project', |
||
115 | blocks: [], |
||
116 | }); |
||
117 | |||
118 | const responseBody = response.json; |
||
119 | |||
120 | expect(response.statusCode).to.equal(201); |
||
121 | expect(responseBody).to.matchPattern(`{ |
||
122 | "_id": String, |
||
123 | "title": String, |
||
124 | "fonts": Array, |
||
125 | "publish": Boolean, |
||
126 | "secure": Boolean, |
||
127 | "countViews": Number, |
||
128 | "timeViews": Number, |
||
129 | "priority": Number, |
||
130 | "blocks": Array, |
||
131 | "userId": String, |
||
132 | "accountId": String, |
||
133 | "token": String, |
||
134 | "slug": String, |
||
135 | "publishURL": String, |
||
136 | "createdAt": String, |
||
137 | "updatedAt": String, |
||
138 | "__v": Number, |
||
139 | }`); |
||
140 | expect(responseBody.title).to.equal('New Project'); |
||
141 | expect(responseBody.blocks).to.be.empty; |
||
142 | |||
143 | testProjectId = responseBody._id; |
||
144 | }); |
||
145 | }); |
||
146 | |||
147 | describe('List', function() { |
||
148 | it('should list user projects paginated', async function() { |
||
149 | const client = await getAuthenticatedTestClient(); |
||
150 | const response = await client.listProjects(); |
||
151 | |||
152 | const responseBody = response.json; |
||
153 | const projects = responseBody.items; |
||
154 | |||
155 | expect(response.statusCode).to.equal(200); |
||
156 | expect(responseBody).to.matchPattern(`{ |
||
157 | "items": Array, |
||
158 | "totalItems": Number, |
||
159 | "page": Number, |
||
160 | "limit": Number, |
||
161 | "pages": Number, |
||
162 | "defaultCover": String, |
||
163 | }`); |
||
164 | projects.forEach(project => { |
||
165 | expect(project).to.matchPattern(`{ |
||
166 | "_id": String, |
||
167 | "title": String, |
||
168 | "fonts": Array, |
||
169 | "publish": Boolean, |
||
170 | "secure": Boolean, |
||
171 | "countViews": Number, |
||
172 | "timeViews": Number, |
||
173 | "priority": Number, |
||
174 | "blocks": Array, |
||
175 | "userId": String, |
||
176 | "accountId": String, |
||
177 | "token": String, |
||
178 | "slug": String, |
||
179 | "publishURL": String, |
||
180 | "createdAt": String, |
||
181 | "updatedAt": String, |
||
182 | "__v": Number, |
||
183 | "password"?: String OR null, |
||
184 | "lastView"?: String, |
||
185 | }`); |
||
186 | }); |
||
187 | }); |
||
188 | |||
189 | it('should list user projects paginated in a specific page', async function() { |
||
190 | const client = await getAuthenticatedTestClient(); |
||
191 | const response = await client.listProjects(2, 3); |
||
192 | |||
193 | const responseBody = response.json; |
||
194 | const projects = responseBody.items; |
||
195 | |||
196 | expect(response.statusCode).to.equal(200); |
||
197 | expect(responseBody).to.matchPattern(`{ |
||
198 | "items": Array, |
||
199 | "totalItems": Number, |
||
200 | "page": Number, |
||
201 | "limit": Number, |
||
202 | "pages": Number, |
||
203 | "defaultCover": String, |
||
204 | }`); |
||
205 | expect(responseBody.page).to.equal(2); |
||
206 | expect(responseBody.limit).to.equal(3); |
||
207 | projects.forEach(project => { |
||
208 | expect(project).to.matchPattern(`{ |
||
209 | "_id": String, |
||
210 | "title": String, |
||
211 | "fonts": Array, |
||
212 | "publish": Boolean, |
||
213 | "secure": Boolean, |
||
214 | "countViews": Number, |
||
215 | "timeViews": Number, |
||
216 | "priority": Number, |
||
217 | "blocks": Array, |
||
218 | "userId": String, |
||
219 | "accountId": String, |
||
220 | "token": String, |
||
221 | "slug": String, |
||
222 | "publishURL": String, |
||
223 | "createdAt": String, |
||
224 | "updatedAt": String, |
||
225 | "__v": Number, |
||
226 | "password"?: String OR null, |
||
227 | "lastView"?: String, |
||
228 | }`); |
||
229 | }); |
||
230 | }); |
||
231 | |||
232 | it('should list user projects paginated with a specific quantity of items per page', async function() { |
||
233 | const client = await getAuthenticatedTestClient(); |
||
234 | const response = await client.listProjects(2); |
||
235 | |||
236 | const responseBody = response.json; |
||
237 | const projects = responseBody.items; |
||
238 | |||
239 | expect(response.statusCode).to.equal(200); |
||
240 | expect(responseBody).to.matchPattern(`{ |
||
241 | "items": Array, |
||
242 | "totalItems": Number, |
||
243 | "page": Number, |
||
244 | "limit": Number, |
||
245 | "pages": Number, |
||
246 | "defaultCover": String, |
||
247 | }`); |
||
248 | expect(responseBody.page).to.equal(2); |
||
249 | projects.forEach(project => { |
||
250 | expect(project).to.matchPattern(`{ |
||
251 | "_id": String, |
||
252 | "title": String, |
||
253 | "fonts": Array, |
||
254 | "publish": Boolean, |
||
255 | "secure": Boolean, |
||
256 | "countViews": Number, |
||
257 | "timeViews": Number, |
||
258 | "priority": Number, |
||
259 | "blocks": Array, |
||
260 | "userId": String, |
||
261 | "accountId": String, |
||
262 | "token": String, |
||
263 | "slug": String, |
||
264 | "publishURL": String, |
||
265 | "createdAt": String, |
||
266 | "updatedAt": String, |
||
267 | "__v": Number, |
||
268 | "password"?: String OR null, |
||
269 | "lastView"?: String, |
||
270 | }`); |
||
271 | }); |
||
272 | }); |
||
273 | |||
274 | it('should list user projects paginated with a specific title search', async function() { |
||
275 | const client = await getAuthenticatedTestClient(); |
||
276 | const response = await client.listProjects(1, 6, 'Project title that does not exist'); |
||
277 | |||
278 | const responseBody = response.json; |
||
279 | const projects = responseBody.items; |
||
280 | |||
281 | expect(response.statusCode).to.equal(200); |
||
282 | expect(responseBody).to.matchPattern(`{ |
||
283 | "items": Array, |
||
284 | "totalItems": Number, |
||
285 | "page": Number, |
||
286 | "limit": Number, |
||
287 | "pages": Number, |
||
288 | "defaultCover": String, |
||
289 | }`); |
||
290 | expect(responseBody.page).to.equal(1); |
||
291 | expect(responseBody.limit).to.equal(6); |
||
292 | expect(responseBody.items.length).to.equal(0); |
||
293 | projects.forEach(project => { |
||
294 | expect(project).to.matchPattern(`{ |
||
295 | "_id": String, |
||
296 | "title": String, |
||
297 | "fonts": Array, |
||
298 | "publish": Boolean, |
||
299 | "secure": Boolean, |
||
300 | "countViews": Number, |
||
301 | "timeViews": Number, |
||
302 | "priority": Number, |
||
303 | "blocks": Array, |
||
304 | "userId": String, |
||
305 | "accountId": String, |
||
306 | "token": String, |
||
307 | "slug": String, |
||
308 | "publishURL": String, |
||
309 | "createdAt": String, |
||
310 | "updatedAt": String, |
||
311 | "__v": Number, |
||
312 | "password"?: String OR null, |
||
313 | "lastView"?: String, |
||
314 | }`); |
||
315 | }); |
||
316 | }); |
||
317 | }); |
||
318 | |||
319 | describe('Retrieve', function() { |
||
320 | it('should retrieve a existing project', async function() { |
||
321 | const client = await getAuthenticatedTestClient(); |
||
322 | const response = await client.listProject(testProjectId); |
||
323 | |||
324 | expect(response.statusCode).to.equal(200); |
||
325 | expect(response.json).to.matchPattern(`{ |
||
326 | "_id": String, |
||
327 | "title": String, |
||
328 | "fonts": Array, |
||
329 | "publish": Boolean, |
||
330 | "secure": Boolean, |
||
331 | "countViews": Number, |
||
332 | "timeViews": Number, |
||
333 | "priority": Number, |
||
334 | "blocks": Array, |
||
335 | "userId": String, |
||
336 | "accountId": String, |
||
337 | "token": String, |
||
338 | "slug": String, |
||
339 | "publishURL": String, |
||
340 | "createdAt": String, |
||
341 | "updatedAt": String, |
||
342 | "__v": Number, |
||
343 | "password"?: String OR null, |
||
344 | "lastView"?: String, |
||
345 | }`); |
||
346 | }); |
||
347 | }); |
||
348 | |||
349 | describe('Update', function() { |
||
350 | it('should update a existing project', async function() { |
||
351 | const client = await getAuthenticatedTestClient(); |
||
352 | const response = await client.updateProject(testProjectId, { |
||
353 | title: 'Updated Project Title', |
||
354 | }); |
||
355 | |||
356 | const responseBody = response.json; |
||
357 | |||
358 | expect(response.statusCode).to.equal(200); |
||
359 | expect(responseBody).to.matchPattern(`{ |
||
360 | "_id": String, |
||
361 | "title": String, |
||
362 | "fonts": Array, |
||
363 | "publish": Boolean, |
||
364 | "secure": Boolean, |
||
365 | "countViews": Number, |
||
366 | "timeViews": Number, |
||
367 | "priority": Number, |
||
368 | "blocks": Array, |
||
369 | "userId": String, |
||
370 | "accountId": String, |
||
371 | "token": String, |
||
372 | "slug": String, |
||
373 | "publishURL": String, |
||
374 | "createdAt": String, |
||
375 | "updatedAt": String, |
||
376 | "__v": Number, |
||
377 | }`); |
||
378 | expect(responseBody.title).to.equal('Updated Project Title'); |
||
379 | }); |
||
380 | }); |
||
381 | |||
382 | describe('Clone', function() { |
||
383 | it('should clone a project and return it', async function() { |
||
384 | const client = await getAuthenticatedTestClient(); |
||
385 | const response = await client.cloneProject(testProjectId); |
||
386 | |||
387 | const responseBody = response.json; |
||
388 | |||
389 | expect(response.statusCode).to.equal(200); |
||
390 | expect(responseBody).to.matchPattern(`{ |
||
391 | "_id": String, |
||
392 | "title": String, |
||
393 | "fonts": Array, |
||
394 | "publish": Boolean, |
||
395 | "secure": Boolean, |
||
396 | "countViews": Number, |
||
397 | "timeViews": Number, |
||
398 | "priority": Number, |
||
399 | "blocks": Array, |
||
400 | "userId": String, |
||
401 | "accountId": String, |
||
402 | "token": String, |
||
403 | "slug": String, |
||
404 | "publishURL": String, |
||
405 | "createdAt": String, |
||
406 | "updatedAt": String, |
||
407 | "__v": Number, |
||
408 | "password"?: String, |
||
409 | }`); |
||
410 | expect(responseBody.title).to.equal('Updated Project Title'); |
||
411 | expect(responseBody.blocks).to.be.empty; |
||
412 | }); |
||
413 | }); |
||
414 | |||
415 | describe('Password', function() { |
||
416 | it('should set project password', async function() { |
||
417 | const client = await getAuthenticatedTestClient(); |
||
418 | const response = await client.setProjectPassword(testProjectId, 'password'); |
||
419 | |||
420 | const responseBody = response.json; |
||
421 | |||
422 | expect(response.statusCode).to.equal(200); |
||
423 | expect(responseBody).to.matchPattern(`{ |
||
424 | "_id": String, |
||
425 | "title": String, |
||
426 | "fonts": Array, |
||
427 | "publish": Boolean, |
||
428 | "secure": Boolean, |
||
429 | "countViews": Number, |
||
430 | "timeViews": Number, |
||
431 | "priority": Number, |
||
432 | "blocks": Array, |
||
433 | "userId": String, |
||
434 | "accountId": String, |
||
435 | "token": String, |
||
436 | "slug": String, |
||
437 | "publishURL": String, |
||
438 | "createdAt": String, |
||
439 | "updatedAt": String, |
||
440 | "__v": Number, |
||
441 | "password": String, |
||
442 | }`); |
||
443 | expect(responseBody.password).to.equal('password'); |
||
444 | }); |
||
445 | |||
446 | it('should check project password', async function() { |
||
447 | const client = await getUnauthenticatedTestClient(); |
||
448 | |||
449 | const correctPasswordResponse = await client.checkProjectPassword( |
||
450 | testProjectId, |
||
451 | 'password' |
||
452 | ); |
||
453 | expect(correctPasswordResponse.statusCode).to.equal(200); |
||
454 | |||
455 | const incorrectPasswordResponse = await client.checkProjectPassword( |
||
456 | testProjectId, |
||
457 | 'password1' |
||
458 | ); |
||
459 | expect(incorrectPasswordResponse.statusCode).to.equal(401); |
||
460 | }); |
||
461 | }); |
||
462 | |||
463 | describe('Publish', function() { |
||
464 | it('should set project publish to true', async function() { |
||
465 | const client = await getAuthenticatedTestClient(); |
||
466 | const response = await client.publishProject(testProjectId); |
||
467 | |||
468 | const responseBody = response.json; |
||
469 | |||
470 | expect(response.statusCode).to.equal(200); |
||
471 | expect(responseBody).to.matchPattern(`{ |
||
472 | "_id": String, |
||
473 | "title": String, |
||
474 | "fonts": Array, |
||
475 | "publish": Boolean, |
||
476 | "secure": Boolean, |
||
477 | "countViews": Number, |
||
478 | "timeViews": Number, |
||
479 | "priority": Number, |
||
480 | "blocks": Array, |
||
481 | "userId": String, |
||
482 | "accountId": String, |
||
483 | "token": String, |
||
484 | "slug": String, |
||
485 | "publishURL": String, |
||
486 | "createdAt": String, |
||
487 | "updatedAt": String, |
||
488 | "__v": Number, |
||
489 | "password"?: String, |
||
490 | }`); |
||
491 | expect(responseBody.publish).to.equal(true); |
||
492 | }); |
||
493 | }); |
||
494 | |||
495 | describe('Secure', function() { |
||
496 | it('should set project secure to true', async function() { |
||
497 | const client = await getAuthenticatedTestClient(); |
||
498 | const response = await client.secureProject(testProjectId); |
||
499 | |||
500 | const responseBody = response.json; |
||
501 | |||
502 | expect(response.statusCode).to.equal(200); |
||
503 | expect(responseBody).to.matchPattern(`{ |
||
504 | "_id": String, |
||
505 | "title": String, |
||
506 | "fonts": Array, |
||
507 | "publish": Boolean, |
||
508 | "secure": Boolean, |
||
509 | "countViews": Number, |
||
510 | "timeViews": Number, |
||
511 | "priority": Number, |
||
512 | "blocks": Array, |
||
513 | "userId": String, |
||
514 | "accountId": String, |
||
515 | "token": String, |
||
516 | "slug": String, |
||
517 | "publishURL": String, |
||
518 | "createdAt": String, |
||
519 | "updatedAt": String, |
||
520 | "__v": Number, |
||
521 | "password"?: String, |
||
522 | }`); |
||
523 | expect(responseBody.secure).to.equal(true); |
||
524 | }); |
||
525 | }); |
||
526 | |||
527 | describe('Cover', function() { |
||
528 | it('should generate project cover', async function() { |
||
529 | const client = await getAuthenticatedTestClient(); |
||
530 | const response = await client.generateProjectCover(testProjectId); |
||
531 | |||
532 | expect(response.statusCode).to.equal(200); |
||
533 | }); |
||
534 | }); |
||
535 | |||
536 | describe('Copy', function() { |
||
537 | it('should generate project based on a default template', async function() { |
||
538 | const templateId = '5cb47ec98497e9001ad9a1b2'; |
||
539 | const client = await getAuthenticatedTestClient(); |
||
540 | const response = await client.createProjectFromTemplate(templateId); |
||
541 | |||
542 | const responseBody = response.json; |
||
543 | |||
544 | expect(response.statusCode).to.equal(200); |
||
545 | expect(responseBody).to.matchPattern(`{ |
||
546 | "_id": String, |
||
547 | "title": String, |
||
548 | "fonts": Array, |
||
549 | "publish": Boolean, |
||
550 | "secure": Boolean, |
||
551 | "countViews": Number, |
||
552 | "timeViews": Number, |
||
553 | "priority": Number, |
||
554 | "blocks": Array, |
||
555 | "userId": String, |
||
556 | "accountId": String, |
||
557 | "token": String, |
||
558 | "slug": String, |
||
559 | "publishURL": String, |
||
560 | "createdAt": String, |
||
561 | "updatedAt": String, |
||
562 | "__v": Number, |
||
563 | "password"?: any, |
||
564 | }`); |
||
565 | }); |
||
566 | }); |
||
567 | |||
568 | describe('View and Notify', function() { |
||
569 | it('should update project last view time and send email with notification only if is not the project owner viewing', async function() { |
||
570 | const client = await getUnauthenticatedTestClient(); |
||
571 | const response = await client.viewProjectAndNotify(testProjectId); |
||
572 | |||
573 | // const responseBody = response.json; |
||
574 | |||
575 | expect(response.statusCode).to.equal(200); |
||
576 | // expect(responseBody.emailSent).to.equal(true); |
||
577 | }); |
||
578 | }); |
||
579 | |||
580 | describe('Templates', function() { |
||
581 | it('should list templates thar the user can use paginated', async function() { |
||
582 | const client = await getAuthenticatedTestClient(); |
||
583 | const response = await client.listTemplates(); |
||
584 | |||
585 | const responseBody = response.json; |
||
586 | const templates = responseBody.items; |
||
587 | |||
588 | expect(response.statusCode).to.equal(200); |
||
589 | expect(responseBody).to.matchPattern(`{ |
||
590 | "items": Array, |
||
591 | "totalItems": Number, |
||
592 | "page": Number, |
||
593 | "limit": Number, |
||
594 | "pages": Number, |
||
595 | "defaultCover": String, |
||
596 | "lastView"?: String, |
||
597 | }`); |
||
598 | templates.forEach(template => { |
||
599 | expect(template).to.matchPattern(`{ |
||
600 | "_id": String, |
||
601 | "title": String, |
||
602 | "fonts": Array, |
||
603 | "publish": Boolean, |
||
604 | "secure": Boolean, |
||
605 | "countViews": Number, |
||
606 | "timeViews": Number, |
||
607 | "priority": Number, |
||
608 | "blocks": Array, |
||
609 | "userId": String, |
||
610 | "accountId": String, |
||
611 | "token": String, |
||
612 | "slug": String, |
||
613 | "publishURL": String, |
||
614 | "createdAt": String, |
||
615 | "updatedAt": String, |
||
616 | "__v": Number, |
||
617 | "password"?: String OR null, |
||
618 | "cover"?: String, |
||
619 | "currency"?: String, |
||
620 | "heating"?: Object, |
||
621 | "lastView": String, |
||
622 | }`); |
||
623 | }); |
||
624 | }); |
||
625 | }); |
||
626 | |||
627 | describe('Remove', function() { |
||
628 | it('should delete a existing project', async function() { |
||
629 | const client = await getAuthenticatedTestClient(); |
||
630 | const newProject = await client.createProject({ |
||
631 | title: 'New Project', |
||
632 | blocks: [], |
||
633 | }); |
||
634 | |||
635 | const project = JSON.parse(newProject.body); |
||
636 | const projectId = project._id; |
||
637 | |||
638 | const response = await client.deleteProject(projectId); |
||
639 | |||
640 | expect(response.statusCode).to.equal(204); |
||
641 | }); |
||
642 | }); |
||
643 | }); |
||
644 | |||
1286 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.